package com.myboot.demo.controller;

import com.myboot.demo.VO.ResultVO;
import com.myboot.demo.converter.OrderForm2OrderMasterConverter;
import com.myboot.demo.dto.OrderDTO;
import com.myboot.demo.enums.ResultEnum;
import com.myboot.demo.exception.SellException;
import com.myboot.demo.form.OrderForm;
import com.myboot.demo.service.BuyerService;
import com.myboot.demo.service.OrderService;
import com.myboot.demo.utils.ResultVOUtil;
import jdk.nashorn.internal.objects.annotations.Getter;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.PastOrPresent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 韦溜溜
 * @Description:买家订单controller
 * @Date: Create in 16:18 2018/12/22
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;
    /**
     * 创建订单
     * @param orderForm
     * @param bindingResult
     * @return
     */
    @PostMapping("/create")
   public ResultVO<Map<String ,String >> create(@Valid OrderForm orderForm,
                                                BindingResult bindingResult){
       if(bindingResult.hasErrors()){
           log.error("【创建订单】参数不争取 orderForm={}",orderForm);
           throw new SellException(ResultEnum.PARAM_ERROR.getCode(),
                   bindingResult.getFieldError().getDefaultMessage());
       }
       OrderDTO orderDTO = OrderForm2OrderMasterConverter.converter(orderForm);
       if(CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){
           log.error("【创建订单】购物车不能为空");
           throw new SellException(ResultEnum.CART_EMPTY);
       }
       OrderDTO createResult = orderService.create(orderDTO);
       Map<String ,String > map = new HashMap<>();
       map.put("orderId",createResult.getOrderId());
       return ResultVOUtil.success(map);
    }
    /**订单列表
     * @param openid
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value = "page",defaultValue = "0") Integer page,
                                         @RequestParam(value = "size",defaultValue = "10") Integer size){
        if(StringUtils.isEmpty(openid)){
            log.error("【查询订单列表】openid为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
         PageRequest pageRequest = PageRequest.of(page,size);
         Page<OrderDTO> orderDTOPage = orderService.findList(openid,pageRequest);
         return ResultVOUtil.success(orderDTOPage.getContent());
    }
    /**
     * 订单详情
     * @param openid
     * @param orderId
     * @return
     */
    @GetMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openid,
                                     @RequestParam("orderId") String orderId ){
       OrderDTO orderDTO =  buyerService.findOne(openid,orderId);
       return ResultVOUtil.success(orderDTO);
    }
    /**
     * 取消订单
     * @param openid
     * @param orderId
     * @return
     */
    @PostMapping("/cancel")
    public ResultVO cancel (@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId ){
        OrderDTO orderDTO =buyerService.cancelOrder(openid, orderId);
        return ResultVOUtil.success();
    }
}
